<template>
  <!-- 放置弹层组件 -->
  <el-dialog
    :title="dialogTitle"
    :visible="showDialog"
    @close="handleCloseDialog"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple } from '@/api/user'
import { reqAddDepartment, reqGetDepartDetail, reqUpdateDepartDetail } from '@/api/departments'
export default {
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    deptList: {
      type: Array,
      required: true
    },
    nodeData: {
      type: Object,
      required: true
    }
  },

  data() {
    const checkNameRepeat = (rule, value, callback) => {
      let res = []
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        }
        res = this.deptList.filter(item => item.pid === this.nodeData.pid)
      } else {
        res = this.deptList.filter(item => item.pid === this.nodeData.id)
      }
      console.log(res)
      const isRepeat = res.some(item => item.name === value)
      isRepeat ? callback(new Error('同级部门已有该名称')) : callback()
    }
    const checkCodeRepeat = (rule, value, callback) => {
      if (this.form.id && value === this.nodeData.code) {
        callback()
        return
      }
      const res = this.deptList.some(item => item.name === value)
      res ? callback(new Error('部门编码已被占，请修改')) : callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门名称只能是1-10个字符', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }

        ],
        code: [{ required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 10, message: '部门编码只能是1-10个字符', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍只能是1-10个字符', trigger: 'blur' }]
      },
      userList: []
      // dialogTitle: '大标题'
    }
  },
  computed: {
    dialogTitle() {
      return this.form.id ? '编辑部门' : '添加子部门'
    }
  },
  methods: {
    // 添加

    handleCloseDialog() {
      this.$emit('update:show-dialog', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    async getSimpleUserList() {
      const { data } = await reqGetEmployeeSimple()
      this.userList = data
      console.log(data, 999)
    },
    async getDepartDetail() {
      const { data } = await reqGetDepartDetail(this.nodeData.id)
      console.log(data)
      this.form = data
      // console.log(data)
    },
    clickSubmit() {
      this.$refs.form.validate(async(flag) => {
        if (!flag) return
        if (this.form.id) {
          const res = await reqUpdateDepartDetail(this.form)
          console.log(res)
          this.$message.success('编辑部门成功')
        } else {
          const res = await reqAddDepartment({
            ...this.form,
            pid: this.nodeData.id
          })
          console.log(res)
          this.$message.success('新增部门成功')
        }
        // 关闭对话框
        this.handleCloseDialog()
        // 重新获取数据
        this.$emit('add-depts')
      })
    }
  }
}
</script>
